import Vue from "vue";
import Router from "vue-router";
import Layout from '@/layout'; // 布局页

Vue.use(Router);

// 通用页面：不需要守卫，可直接访问
export const constRoutes = [
  {
    path: "/login",
    component: () => import("@/views/Login"),
    hidden: true // 导航菜单忽略该项
  },
  {
    path: "/",
    component: Layout,// 应用布局
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/dashboard"),
        name: "dashboard",
        meta: { title: '控制台', icon: 'dashboard', breadcrumb: false },
      },

    ]
  }
];

// 权限页面：受保护页面，要求用户登录并拥有访问权限的角色才能访问
export const asyncRoutes = [
  {
    path: "/commodity",
    component: Layout,
    redirect: "/commodity/management",
    meta: {
      title: "商品",
      icon: "commodity"
    },
    children: [
      {
        path: "management",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/management"),
        name: "management",
        meta: {
          title: "商品管理",
          roles: ['admin']
        },
      },
      {
        path: "add",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/add"),
        name: "add",
        hidden: true,
        meta: {
          title: "添加商品",
          roles: ['admin']
        },
      },
      {
        path: "class",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/class"),
        name: "class",
        meta: {
          title: "商品分类",
          roles: ['admin']
        },
      },
      {
        path: "specs",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/specs"),
        name: "specs",
        meta: {
          title: "商品规格",
          roles: ['admin']
        },
      },
      {
        path: "comments",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/comments"),
        name: "comments",
        meta: {
          title: "商品评价",
          roles: ['admin']
        },
      },
      {
        path: "broadcast",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/broadcast"),
        name: "broadcast",
        meta: {
          title: "开启直播",
          roles: ['admin']
        },
      },
      {
        path: "livebroadcast",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/liveBroadcast"),
        name: "liveBroadcast",
        meta: {
          title: "观看直播",
          roles: ['admin']
        },
      }
    ]
  },
];

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default new Router({
  // mode: "history",
  base: process.env.BASE_URL,
  routes: constRoutes
});
